package com.reptile.config;

import java.util.HashSet;
import java.util.Set;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;

/*
 * Redis集群对象
 * 
 */

@Configuration
public class RedisConfig
{

	//注入集群节点信息
	@Value("${spring.redis.cluster.nodes}")
	private String clusterNodes;
	
	
	@Bean
	public JedisCluster getJedisCluster()
	{
		//去掉逗号
		String[] cNodes = clusterNodes.split(",");
		
		Set<HostAndPort> nodes = new HashSet<HostAndPort>();
		
		for(String node : cNodes)
		{
			//去掉冒号
			String[] hp =node.split(":");
			nodes.add(new HostAndPort(hp[0],Integer.parseInt(hp[1])));
		}
		
		
		//创建Redis集群对象
		JedisCluster jedisCluster=new JedisCluster(nodes);
		
		return jedisCluster;
	}
	
		
}
